package com.lw.test.controller;

import com.lw.test.entity.ValidatorVO;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotEmpty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Request Param 方式传参校验
 *
 * @author LiuWang
 */
@Slf4j
@RestController
@RequestMapping("/param")
public class RequestParamValidateController {

  @GetMapping
  public ValidatorVO param(@Validated ValidatorVO vo) {
    return vo;
  }

  @GetMapping("/1")
  public String param1(@RequestParam @NotEmpty String name) {
    return name;
  }

  @GetMapping("/2")
  public ValidatorVO param2(ValidatorVO vo) {
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<ValidatorVO>> validateResult = validator.validate(vo);
    for (ConstraintViolation msg : validateResult) {
      log.error(msg.getMessage());
    }
    return vo;
  }
}
